<?php
/**
 * 简易文件上传类
 * User: xiaee
 * Date: 4/22/11
 * Time: 9:43 AM
 */

//$upload_path = getcwd().'/../../uploads/';
$upload_path = getcwd().'/../uploads/';

/**
 * 单个文件最大尺寸: 10Mb
 */
$file_size_allowed = 10485760;

/**
 * 允许的文件MimeType
 */
$mime_type_allowed = array(
    'text/plain', 'text/html', 'text/xml',
    'image/jpeg', 'image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/x-png', 'image/bmp',
    'application/x-compressed', 'application/x-zip-compressed', 'application/x-gzip-compressed', 'application/x-rar-compressed', 'application/zip', 'application/mspowerpoint',
    'application/octet-stream', 'application/msword', 'xlt application/vnd.ms-excel', 'application/msexcel', 'application/msword', 'application/mspowerpoint'
);


/**
 * 随机生成一个合法的文件名
 *
 * @param null $opt_length
 * @return string
 */
function file_random_name($opt_length = null) {
    $alpha = 'abcdefghijklmnopqrstuvwxyz_-';
    $decimal = '0123456789';

    $i = 0;
    $rv = array();
    $dict = $alpha;
    $length = !is_null($opt_length) ? $opt_length : 16;
    do {
        $ch = $dict[mt_rand(0, strlen($dict) - 1)];
        $rv[] = $ch;
        if (!$i) {
            $dict .= $decimal;
        }
    } while ($i++ < $length);

    return implode('', $rv);
}

/**
 * 标志是否有文件上传错误
 */
$has_upload_error = 0;
$upload_files = array();
foreach ($_FILES as $uf) {
    $empty_file = empty($uf['name']) || $uf['size'] <= 0;
    $file_upload_error = !$empty_file && (!in_array($uf['type'], $mime_type_allowed) || $uf['error'] > 0);
    $has_upload_error |= (int)$file_upload_error;
    if ($empty_file || $file_upload_error) {
        continue;
    }

    $file_extension = pathinfo($uf['name'], PATHINFO_EXTENSION);
    $file_name = file_random_name(32).'.'.$file_extension;
    if (!@move_uploaded_file($uf['tmp_name'], $upload_path.$file_name)) {
        $has_upload_error = true;
        continue;
    }

    @chmod($upload_path.$file_name, 0644);
    $upload_files[] = array(
        'name' => $file_name,
        'size' => $uf['size'],
        'path' => '',
        'extension' => $file_extension
    );
}

/**
 * 删除不再使用的变量
 */
unset($file_upload_error, $empty_file, $file_extension, $file_name);


echo '<pre>';
echo 'There is some error? '.($has_upload_error ? 'Yes' : 'No')."\n";
var_export($upload_files);
echo '</pre>';













exit;


$POST_MAX_SIZE = ini_get('post_max_size');
$size_units = array(
    'K' => 1 << 10,
    'M' => 1 << 20,
    'G' => 1 << 30
);

$upload_path = getcwd().'/../uploads/';

$file_size_allowed = 10485760;
$mime_type_allowed = array(
    'image/jpeg'
);

$with_error = false;
$upload_files = array();
foreach ($_FILES as $uf) {
    if (empty($uf['name']) || $uf['size'] <= 0) {
        continue;
    }

    if (!in_array($uf['type'], $mime_type_allowed) || $uf['error'] > 0 ||
        $uf['size'] > $file_size_allowed) {
        $with_error = true;
        continue;
    }

    $pi = pathinfo($uf['name']);
    $file_extension = $pi['extension'];
    $file_name = file_random_name(32).'.'.$file_extension;
    if (!@move_uploaded_file($uf['tmp_name'], $upload_path.$file_name)) {
        $with_error = true;
        continue;
    }

    $upload_files[] = array(
        'name' => $file_name,
        'size' => $uf['size']
    );
}


/*function file_random_name($opt_length = null) {
    $alpha = 'abcdefghijklmnopqrstuvwxyz_-';
    $decimal = '0123456789';

    $i = 0;
    $rv = array();
    $dict = $alpha;
    $length = !is_null($opt_length) ? $opt_length : 16;
    do {
        $ch = $dict[mt_rand(0, strlen($dict) - 1)];
        $rv[] = $ch;
        if (!$i) {
            $dict .= $decimal;
        }
    } while ($i++ < $length);

    return implode('', $rv);
}*/



echo '<pre>';
var_export($_FILES);
echo '</pre>';
